// models/commentModel.js
const pool = require('../config/db');
// 获取博客的所有评论
const getCommentsByBlogId = async (blog_id) => {
  const [rows] = await pool.query('SELECT * FROM comments WHERE blog_id = ?', [blog_id]);
  return rows;
};
// 通过user获取博客ID
// 获取评论的博客ID
const getBlogIdByCommentId = async (comment_id) => {
  const [rows] = await pool.query('SELECT blog_id FROM comments WHERE comment_id = ?', [comment_id]);
  return rows[0].blog_id;
};
//通过评论ID获取用户ID再通过用户ID获取用户名  
const getUserNameByCommentId = async (comment_id) => {
  
}
// 添加评论
const createComment = async (blog_id, user_id,  content) => {
  const [result] = await pool.query('INSERT INTO comments (blog_id, user_id,  content) VALUES (?, ?,  ?)', [blog_id, user_id,  content]);
  return result.insertId;
};


// 删除评论
const deleteComment = async (comment_id) => {
  await pool.query('DELETE FROM comments WHERE comment_id = ?', [comment_id]);
};
// // 回复评论
// const replyComment = async (comment_id, user_id, username, content, reply_to) => {
//   const [result] = await pool.query('INSERT INTO comments (blog_id, user_id, username, content, reply_to) SELECT blog_id, ?, ?, ? FROM comments WHERE comment_id = ?', [user_id, username, content, comment_id]);
//   return result.insertId;
// };
// 回复评论
const replyComment = async (blog_id, user_id,  content, reply_to) => {
  const [result] = await pool.query('INSERT INTO comments (blog_id, user_id, content, reply_to) VALUES (?,  ?, ?, ?)', [blog_id, user_id, content, reply_to]);
  return result.insertId;
};
module.exports = {
  getCommentsByBlogId,
  createComment,
  deleteComment,
  replyComment,
  getBlogIdByCommentId,
};
// const pool = require('../config/db');

// // 获取博客的所有评论
// const getCommentsByBlogId = async (blog_id) => {
//   const [rows] = await pool.query('SELECT * FROM comments WHERE blog_id = ?', [blog_id]);
//   return rows;
// };

// // 获取评论的博客ID
// const getBlogIdByCommentId = async (comment_id) => {
//   const [rows] = await pool.query('SELECT blog_id FROM comments WHERE comment_id = ?', [comment_id]);
//   return rows[0]?.blog_id; // 确保返回的结果存在
// };

// // 添加评论
// const createComment = async (blog_id, user_id, username, content) => {
//   const [result] = await pool.query('INSERT INTO comments (blog_id, user_id, username, content) VALUES (?, ?, ?, ?)', [blog_id, user_id, username, content]);
//   return result.insertId;
// };

// // 删除评论
// const deleteComment = async (id) => {
//   await pool.query('DELETE FROM comments WHERE comment_id = ?', [id]);
// };

// // 回复评论
// const replyComment = async (blog_id, user_id, username, content, reply_to) => {
//   const [result] = await pool.query('INSERT INTO comments (blog_id, user_id, username, content, reply_to) VALUES (?, ?, ?, ?, ?)', [blog_id, user_id, username, content, reply_to]);
//   return result.insertId;
// };

// module.exports = {
//   getCommentsByBlogId,
//   createComment,
//   deleteComment,
//   replyComment,
//   getBlogIdByCommentId,
// };
